﻿using System;
using Glue;
using Glue.Internals;
using GlueTests.Builders;
using Xunit;

namespace GlueTests.RelationContainerTests
{
    public class When_relating_a_property_with_flattening
    {
        private readonly Mapping<DomainPerson, GuiPerson> mapping;
        private readonly RelationContainer<DomainPerson, GuiPerson> relations;

        public class DomainPerson
        {
            public DomainAddress Address { get; set; }
            public String DummyCity { get; set; }
        }
        public class DomainAddress { public String City { get; set; } }
        
        public class GuiPerson
        {
            public String City { get; set; }
        }

        public When_relating_a_property_with_flattening()
        {
            var addressMapping = new Mapping<DomainAddress, GuiPerson>();
            addressMapping.Relate(domain=>domain.City,gui=>gui.City);
            
            mapping = new Mapping<DomainPerson, GuiPerson>();
            mapping.Flatten(domain=>domain.Address,addressMapping);
            relations = mapping.Relations;
        }

        [Fact]
        public void Should_contain_one_relation()
        {
            Assert.Equal(1, relations.Count);
        }

        [Fact]
        public void Should_contain_flattened_property_OnRightSide()
        {
            Assert.True(relations.IsRelatedTowardsOnRightSide(new PropertyBuilder(typeof(DomainAddress), "City").Build()));
        }

        [Fact]
        public void Should_contain_nested_property_OnLeftSide()
        {
            Assert.True(relations.IsRelatedTowardsOnLeftSide(new PropertyBuilder(typeof(DomainPerson), "Address").Build()));
        }

        // [Fact] //TODO: Should I?
        public void Should_override_flattened_property_when_new_relation_is_added_for_that_property()
        {
            Assert.Equal(1, relations.Count);
            mapping.Relate(domain=>domain.DummyCity,gui=>gui.City);
            Assert.Equal(1, relations.Count);
        }

        
    }
}
